<?php
/*
  $Id: $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2007 osCommerce

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License v2 (1991)
  as published by the Free Software Foundation.
*/

  header('Cache-Control: no-cache, must-revalidate');
  header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

  require('includes/application.php');

  $dir_fs_www_root = dirname(__FILE__);

  if (isset($_GET['action']) && !empty($_GET['action'])) {
    switch ($_GET['action']) {
      case 'dbCheck':
        $db = array('DB_SERVER' => trim(urldecode($_GET['server'])),
                    'DB_SERVER_USERNAME' => trim(urldecode($_GET['username'])),
                    'DB_SERVER_PASSWORD' => trim(urldecode($_GET['password'])),
                    'DB_DATABASE' => trim(urldecode($_GET['name'])),
                    'DB_DATABASE_CLASS' => trim(urldecode($_GET['class']))
                   );

        $osC_Database = osC_Database::connect($db['DB_SERVER'], $db['DB_SERVER_USERNAME'], $db['DB_SERVER_PASSWORD'], $db['DB_DATABASE_CLASS']);

        if ($osC_Database->isError() === false) {
          $osC_Database->selectDatabase($db['DB_DATABASE']);
        }

        if ($osC_Database->isError()) {
          echo '[[0|' . $osC_Database->getError() . ']]';
        } else {
          echo '[[1]]';
        }

        exit;
        break;

      case 'dbImport':
        $db = array('DB_SERVER' => trim(urldecode($_GET['server'])),
                    'DB_SERVER_USERNAME' => trim(urldecode($_GET['username'])),
                    'DB_SERVER_PASSWORD' => trim(urldecode($_GET['password'])),
                    'DB_DATABASE' => trim(urldecode($_GET['name'])),
                    'DB_DATABASE_CLASS' => trim(urldecode($_GET['class'])),
                    'DB_TABLE_PREFIX' => trim(urldecode($_GET['prefix']))
                   );

        $osC_Database = osC_Database::connect($db['DB_SERVER'], $db['DB_SERVER_USERNAME'], $db['DB_SERVER_PASSWORD'], $db['DB_DATABASE_CLASS']);

        if ($osC_Database->isError() === false) {
          $osC_Database->selectDatabase($db['DB_DATABASE']);
        }

        if ($osC_Database->isError() === false) {
      $sql_file = $dir_fs_www_root . '/upgrade_1.0_en_US.sql';
          $osC_Database->importSQL($sql_file, $db['DB_DATABASE'], $db['DB_TABLE_PREFIX']);
        }

        if ( ($osC_Database->isError() === false) && ($db['DB_DATABASE_CLASS'] == 'mysqli_innodb') ) {
          $Qinno = $osC_Database->query('show variables like "have_innodb"');
          if (($Qinno->numberOfRows() === 1) && (strtolower($Qinno->value('Value')) == 'yes')) {
            $database_tables = array('address_book', 'categories', 'categories_description', 'customers', 'manufacturers', 'manufacturers_info', 'orders', 'orders_products', 'orders_status', 'orders_status_history', 'orders_products_attributes', 'orders_products_download', 'orders_total', 'products', 'products_attributes', 'products_attributes_download', 'products_description', 'products_options', 'products_options_values', 'products_options_values_to_products_options', 'products_to_categories', 'reviews', 'shopping_carts', 'shopping_carts_custom_variants_values', 'weight_classes', 'weight_classes_rules');

            foreach ($database_tables as $table) {
              $osC_Database->simpleQuery('alter table ' . $db['DB_TABLE_PREFIX'] . $table . ' type = innodb');
            }
          }
        }

        if ($osC_Database->isError()) {
          echo '[[0|' . $osC_Database->getError() . ']]';
        } else {
          echo '[[1]]';
        }

        exit;
        break;
    }
  }

  echo '[[-100|noActionError]]';
?>
